package com.enba.boot.mq.sender.activemq;

import com.enba.boot.mq.MessageInteface;
import com.enba.boot.mq.MessageSenderStrategy;
import com.enba.boot.mq.properties.MqProperties;
import com.enba.boot.mq.sender.activemq.config.ActiveMQConfig;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;

/** actibqmq 集群模式消息发送 */
public class ActiveMqClusterMessageSenderStrategy
    implements MessageSenderStrategy<ActiveMqMessageBase> {

  @Resource(name = ActiveMQConfig.CLUSTER_TEMPLATE_BEAN_NAME)
  private JmsMessagingTemplate jmsTemplateQueue;

  @Resource(name = ActiveMQConfig.BROADCAST_TEMPLATE_BEAN_NAME)
  private JmsMessagingTemplate jmsTemplateTopic;

  @Autowired private MqProperties mqProperties;

  @Override
  public String getStrategyKey() {
    return mqProperties.getActivemq().getStrategyKey();
  }

  @Override
  public void sendMessageByQueue(String destinationName, String message) {
    logger.info(
        "ActiveMqMessageSenderStrategy.sendMessageByQueue destinationName:{}, message:{}",
        destinationName,
        message);

    jmsTemplateQueue.convertAndSend(destinationName, message);
  }

  @Override
  public void sendMessageByTopic(String topicName, String message) {

    logger.info(
        "ActiveMqMessageSenderStrategy.sendMessageByTopic topicName:{}, message:{}",
        topicName,
        message);
    jmsTemplateTopic.convertAndSend(topicName, message);
  }

  @Override
  public void sendMessageByTopic(String topicName, String message, String key) {

  }

  @Override
  public void sendMessageByQueue(
      String destinationName, MessageInteface<ActiveMqMessageBase> messageInteface) {
    // 消息业务唯一标识
    String bizId = messageInteface.getBizId();
    // 消息业务内容
    ActiveMqMessageBase bizBody = messageInteface.getBizBody();

    logger.info(
        "ActiveMqMessageSenderStrategy.sendMessageByQueue bizId:{}, bizBody:{}", bizId, bizBody);

    jmsTemplateQueue.convertAndSend(destinationName, messageInteface);
  }

  @Override
  public void sendMessageByTopic(
      String topicName, MessageInteface<ActiveMqMessageBase> messageInteface) {
    // 消息业务唯一标识
    String bizId = messageInteface.getBizId();
    // 消息业务内容
    ActiveMqMessageBase bizBody = messageInteface.getBizBody();

    logger.info(
        "ActiveMqMessageSenderStrategy.sendMessageByTopic bizId:{}, bizBody:{}", bizId, bizBody);

    jmsTemplateTopic.convertAndSend(topicName, messageInteface);
  }
}
